﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.CodeParser;
using DevExpress.XtraEditors;
using Zero.POCO.Enum;
using Zero.POCO.Enum.Helper.Support;
using Zero.POCO.Helper.Support;
using Zero.POCO.Support;
using Zero.Presenter.Donnees;

namespace DevExpress.MailClient.Win.Forms.Data
{
    public partial class frmEditTarif : XtraForm
    {

        private bool _isTarifViewModified ;
        private bool _newTarifView { get; set; }
        public TarifView TarfView { get; set; }
        public Guid TarifViewId { get; set; }
        List<SupportTV> listeSupportTvs { get; set; }
        List<SupportPresse> listeSupportPresses { get; set; }
        List<SupportRadio> listesSupportRadios { get; set; }
        List<string> _supports = new List<string>();
        public frmEditTarif(TarifView tarifViewHist,bool newTarifview, List<SupportTV> supportTvs, List<SupportPresse> supportPresses, List<SupportRadio> supportRadios)
        {
            InitializeComponent();
            DialogResult = DialogResult.Cancel;
            listeSupportPresses = supportPresses;
            listeSupportTvs = supportTvs;
            listesSupportRadios = supportRadios;
            gcSupport.DataSource = _supports;
            
            luMois.Properties.DataSource = Enum.GetValues(typeof(Mois));

            int anneTemp = 2000;
            List<int>ListTemp=new List<int>();
            while (anneTemp < 2021){ListTemp.Add(anneTemp);
                anneTemp++;
            }
            luAnne.Properties.DataSource = ListTemp;
            _newTarifView = newTarifview;
            if (_newTarifView)
            {
                TarifViewId = default(Guid);
                _isTarifViewModified = true;
            }
            else
            {
                TarifViewId = tarifViewHist.id;}
            cbMedia.SelectedIndex = 0;
            this.cbMedia_SelectedIndexChanged(null, null);
            luSupport.ItemIndex = 0;
            _supports = new List<string>();

        }
        DialogResult QueryDelete()
        {
            DialogResult result = XtraMessageBox.Show(this, Properties.Resources.DeleteQuestion, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
            switch (result)
            {
                case DialogResult.Cancel:
                    return DialogResult.Cancel;
                case DialogResult.No:
                    return DialogResult.No;
                default:
                    DeleteRecord();
                    return DialogResult.Yes;
            }
        }

        private void DeleteRecord()
        {
            throw new NotImplementedException();
        }


        DialogResult QueryClose()
        {
            if (!_isTarifViewModified)
                return DialogResult.Yes;

            DialogResult result = XtraMessageBox.Show(this, Properties.Resources.SaveQuestion, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
            switch (result)
            {
                case DialogResult.Cancel:
                    return DialogResult.Cancel;
                case DialogResult.No:
                    return DialogResult.No;
                default:
                    ApplyChanges();
                    return DialogResult.Yes;
            }
        }

        private void ApplyChanges()
        {
            TarfView = new TarifView()
            {
                Nom = TENom.Text,
                
                Remarque = meRemarque.Text,
                id = TarifViewId,
                

            };
            int anneTemp;
            var testAnne = int.TryParse(luAnne.Text, out anneTemp);
            if (testAnne)
                TarfView.annee = anneTemp;

            TarfView.SupportsNom = new List<string>();
            TarfView.SupportsNom.AddRange(_supports);

            Mois tempmois;
              var testMois=Mois.TryParse(luMois.Text, out tempmois);
            if(testMois)
                TarfView.mois = tempmois;
            
            TypeMedia media;
            TypeMedia.TryParse(cbMedia.SelectedText, out media);
            TarfView.media = media;


        }

    private void btnSauver_Click(object sender, EventArgs e)
        {
            _isTarifViewModified = false;
            DialogResult result = QueryClose();
            ApplyChanges();
            if (result == DialogResult.Yes) this.Close();
        }

        private void cbMedia_SelectedIndexChanged(object sender, EventArgs e)
        {
            _isTarifViewModified = true;
            switch (cbMedia.SelectedIndex)
            {
                case 0:
                {
                    luSupport.Properties.DataSource = listeSupportTvs;
                    luSupport.Properties.ValueMember = "id";
                    luSupport.Properties.DisplayMember = "Libelle";
                   

                    luSupport.ItemIndex = 0;
                    gcSupport.DataSource = _supports;
                    _supports = new List<string>();
                    break;
                }
                case 1:
                {
                    luSupport.Properties.DataSource = listesSupportRadios;
                    luSupport.Properties.ValueMember = "id";
                    luSupport.Properties.DisplayMember = "Libelle";

                    luSupport.ItemIndex = 0;
                    gcSupport.DataSource = _supports;
                    _supports = new List<string>();
                    break;
                }
                case 2:
                {
                    luSupport.Properties.DataSource = listeSupportPresses;
                    luSupport.Properties.ValueMember = "id";
                    luSupport.Properties.DisplayMember = "Libelle";
                    _supports = new List<string>();
                    gcSupport.DataSource = _supports;

                    luSupport.ItemIndex = 0;break;
                }
                    
            }
        }
        private void luSupport_Popup(object sender, EventArgs e)
        {
        
        }

        private void luSupport_Properties_Popup(object sender, EventArgs e)
        {
            luSupport.Properties.Columns[0].Visible = false;
            luSupport.Properties.Columns[1].Visible = false;

            luSupport.Properties.Columns[2].Visible = false;
            luSupport.Properties.Columns[3].Visible = false;
            luSupport.Properties.Columns[5].Visible = false;
        }

        private void textEdit1_Properties_Validating(object sender, CancelEventArgs e)
        {
            if (TENom.Text.Trim().Length <= 0)
                e.Cancel = true;
        }

        private void TENom_TextChanged(object sender, EventArgs e)
        {
            _isTarifViewModified = true;
        }

        private void luAnne_EditValueChanged(object sender, EventArgs e)
        {
            _isTarifViewModified = true;
        }

        private void luMois_EditValueChanged(object sender, EventArgs e)
        {
            _isTarifViewModified = true;
        }

        private void luSupport_TextChanged(object sender, EventArgs e)
        {
            _isTarifViewModified = true;
        }

        private void memoEdit1_TextChanged(object sender, EventArgs e)
        {
            _isTarifViewModified = true;
        }

  
        private void barButtonItem1_ItemClick(object sender, XtraBars.ItemClickEventArgs e)
        {
            if(luSupport.Text==null) return;
            if (_supports.Contains(luSupport.Text)) return;
            if (luSupport.Text == luSupport.Properties.NullText) return;
           _supports.Add(luSupport.Text);
            
           gcSupport.DataSource = _supports;
            gcSupport.RefreshDataSource();
        }

        private void barButtonItem2_ItemClick(object sender, XtraBars.ItemClickEventArgs e)
        {
            if (luSupport.Text == null) return;
            if (!_supports.Contains(luSupport.Text)) return;
            _supports.Remove(luSupport.Text);
            gcSupport.DataSource = _supports;
            gcSupport.RefreshDataSource();
        }



    }
}